﻿using Hospital.IService;
using Hospital.Model.entity;
using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hospital.WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class HospitalRoomController : ControllerBase
    {
        private readonly IWardService _iWardService;

        public HospitalRoomController(IWardService iHospitalRoomService)
        {
            _iWardService = iHospitalRoomService;
        }
        [HttpGet]
        public async Task<ActionResult<ApiResult>> GetHospitalRooms()
        {
            var rooms = await _iWardService.QueryAsync();
            if (rooms != null) return ApiResultHelper.Success(rooms);
            return ApiResultHelper.Error("error");
        }
        [HttpPost]
        public async Task<ActionResult<ApiResult>> PostHospitalRoom([FromBody] Ward ward)
        {
            bool b = await _iWardService.CreateAsync(ward);
            if (b) return ApiResultHelper.Success("success");
            return ApiResultHelper.Error("error");
        }
        [HttpDelete("{id}")]
        public async Task<ActionResult<ApiResult>> DeleteHospitalRoom(int id)
        {
            bool b = await _iWardService.DeleteAsync(id);
            if (b) return ApiResultHelper.Success("success");
            return ApiResultHelper.Error("error");
        }
        [HttpPut("{id}")]
        public async Task<ActionResult<ApiResult>> PutHospitalRoom(int id, [FromBody] Ward ward)
        {
            if (id != ward.Id) return ApiResultHelper.Error("error request");
            bool b = await _iWardService.EditAsync(ward);
            if (b) return ApiResultHelper.Success("success");
            return ApiResultHelper.Error("error");
        }
    }
}
